using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using OpenComposite.Base.Collections;
using System.Globalization;

namespace OpenComposite.EII.KPI
{
	public partial class FormKPIMetric : Form
	{
		public FormKPIMetric(Repository.KPIMetric metric)
		{
			_metric = metric;

			InitializeComponent();

			this.DialogResult = DialogResult.Cancel;
		}

		private Repository.KPIMetric _metric;

		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			close(DialogResult.OK);	
		}

		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			close(DialogResult.Cancel);
		}

		private void FormNewKPIMetric_Load(object sender, EventArgs e)
		{
			//DataTable dtKPI = Global.Data.SelectRepositoryTableComplete(OpenComposite.EII.Repository.ItemType.KPI);
			//DataRow[] rows = dtKPI.Select(string.Format("org_id = 0 OR org_id = {0}", Global.CurrentUser.OrganizationID));
			//dtKPI.DefaultView.RowFilter = string.Format("org_id = 0 OR org_id = {0}", Global.CurrentUser.OrganizationID);
			BindingListEx<KeyPerformanceIndicator> kpis = Global.Data.GetKeyPerformanceIndicatiors();
			cmbKPI.DisplayMember = "Name";
			cmbKPI.ValueMember = "ID";
			cmbKPI.DataSource = kpis;
			cmbKPI.SelectedIndex = 0;

			if ( _metric.KpiID > 0 )
				cmbKPI.SelectedValue = _metric.KpiID;

			if ( _metric.Kpi != null ) {
				txtValue.Text = 
					Helpers.ConvertUnit(_metric.Value, _metric.Kpi.BaseUnit, 
						_metric.Kpi.GetKpiUnit(_metric.Unit)).ToString(CultureInfo.InvariantCulture);
			}
			cmbDataType.SelectedItem = _metric.Unit;
			txtDesc.Text = _metric.Description;
		}

		private void cmbKPI_SelectedIndexChanged(object sender, EventArgs e)
		{
			cmbDataType.BeginUpdate();
			if ( cmbKPI.SelectedIndex >= 0 && cmbKPI.SelectedItem != null ) {

				KeyPerformanceIndicator kpi = Global.Data.RepositoryService.GetItem<KeyPerformanceIndicator>((int)cmbKPI.SelectedValue);
				if ( kpi != null ) {
					cmbDataType.DataSource = kpi.ExtendedProperties.Units;
				}
			}
			cmbDataType.EndUpdate();
		}

		private void btnAddNew_Click(object sender, EventArgs e)
		{
			IRepositoryItem parent = null;
			KeyPerformanceIndicator kpi = Global.Data.RepositoryService.GetNewItem<KeyPerformanceIndicator>(true, ref parent);
			if ( kpi == null ) return;
			kpi.Save();
			DataTable dtKPI = Global.Data.SelectRepositoryTableComplete(OpenComposite.EII.Repository.ItemType.KPI);
			//DataRow[] rows = dtKPI.Select(string.Format("org_id = 0 OR org_id = {0}", Global.CurrentUser.OrganizationID));
			dtKPI.DefaultView.RowFilter = string.Format("org_id = 0 OR org_id = {0}", Global.CurrentUser.OrganizationID);
			cmbKPI.DisplayMember = "kpi_nm";
			cmbKPI.ValueMember = "kpi_id";
			cmbKPI.DataSource = dtKPI.DefaultView;
			cmbKPI.SelectedItem = kpi.ID;
		}

		private void close(DialogResult result)
		{
			if ( result == DialogResult.OK ) {
				if ( string.IsNullOrEmpty(txtValue.Text) ) {
					MessageBox.Show("Please enter a value!",
						"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					return;
				}
				if ( _metric != null ) {
					_metric.KpiID = (int)cmbKPI.SelectedValue;
					_metric.Name = cmbKPI.Text;
					_metric.Description = txtDesc.Text;
					KpiUnit unit = (KpiUnit)cmbDataType.SelectedItem;
					_metric.Value = Helpers.ConvertUnit(txtValue.Text, unit, _metric.Kpi.BaseUnit).ToString(CultureInfo.InvariantCulture);
					_metric.Unit = cmbDataType.Text;

					_metric.Save();
				}
			}
			this.DialogResult = result;
			Close();
		}

		private void txtValue_KeyPress(object sender, KeyPressEventArgs e)
		{
		}

		private void txtValue_KeyUp(object sender, KeyEventArgs e)
		{
			if ( e.KeyCode == Keys.Enter ) {
				close(DialogResult.OK);
			}
		}

		private void cmbDataType_KeyUp(object sender, KeyEventArgs e)
		{
			if ( e.KeyCode == Keys.Enter ) {
				close(DialogResult.OK);
			}
		}

		private void txtDesc_KeyUp(object sender, KeyEventArgs e)
		{
			if ( e.Control && e.KeyCode == Keys.Enter ) {
				e.Handled = true;
				close(DialogResult.OK);
			}
		}

	}
}